package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.LocationDriver;
import com.bocloud.cmp.driver.providers.ali.AliLocationDriver;
import com.bocloud.cmp.driver.providers.intf.LocationProvider;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.providers.os.OSLocationDriver;
import com.bocloud.cmp.driver.providers.vmware.VmwareLocationDriver;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.common.model.BsmResult;

/**
 * 位置服务实现类
 * 
 * @author dmw
 *
 */
@Service("locationProvider")
public class LocationProviderImpl implements LocationProvider {

	@Override
	public BsmResult listRegions(String vendor) {
		BsmResult result = null;
		LocationDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case ALIYUN:
			driver = new AliLocationDriver(butler.getAccessKey(), butler.getSecretKey(), "cn-hangzhou");
			result = driver.listRegions();
			break;
		case VMWARE:
			driver = new VmwareLocationDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			result = driver.sync();
			break;
		default:
			result = new BsmResult(false, "暂不支持！");
			break;
		}
		return result;
	}

	@Override
	public BsmResult listZones(String vendor, String region) {
		BsmResult result = null;
		LocationDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case ALIYUN:
			driver = new AliLocationDriver(butler.getAccessKey(), butler.getSecretKey(), region);
			result = driver.listZones(region);
			break;
		default:
			result = new BsmResult(false, "暂不支持！");
			break;
		}
		return result;
	}

	@Override
	public BsmResult sync(String vendor) {
		Butler butler = Configuration.instance().getConfig(vendor);
		LocationDriver driver = null;
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case ALIYUN:
			driver = new AliLocationDriver(butler.getAccessKey(), butler.getSecretKey(), "cn-hangzhou");
			break;
		case OPENSTACK:
			driver = new OSLocationDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(),
					butler.getTenant(), butler.getDomainName(), butler.getProjectName(),
					Version.valueOf(butler.getVersion()));
			break;
		case VMWARE:
			driver = new VmwareLocationDriver(butler.getUrl(), butler.getUsername(), butler.getPassword());
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.sync();
		}
	}

}
